
import { observable, action } from 'mobx';
import { ListViewPage, Form } from 'weaver-mobile-page';
import { Tools } from 'weaver-mobile';
import { initPage } from "../../apis/ecme";

export default class OthersStore {
    @observable form = new Form.Store({ route: '/index/card' });
    @observable feaParam = {};
    @observable fieldMap = {};
    @action
    init = (params) => {
        this.feaParam = params;
        initPage(params).then(data => {
            this.fieldMap = data.fieldMap;
            let scriptSrc = data.scriptSrc;
            this.form.initForm(data.condition, params.rute);
            document.getElementById("custompageCodeArea").innerHTML = "<style type=\"text/css\">"+scriptSrc+"</style>";
            try {
                if (params.isecmeview && params.isecmeview === "1") {
                    window.parent.ecmeLayoutCacheMobile(params.type);
                }
            } catch (e) {
                console.log(e);
            }
        })
    }

    handleSubmit = () => {
        let result = this.form.doValidate();
        let data = this.form.formParams;
        let JSONStr = {
            detailTable: {},
            extendDatas: { exdetailTable: {}, exmainTable: {} },
            mainTable: {}
        };
        for (let key in data) {
            if (key.indexOf("span") == -1) {
                let value = data[key];
                let fieldid = key.replace("field", "");
                let fieldObj = (this.fieldMap || {})[fieldid];
                let { issystem, tableid, tablename, fieldhtmltype, fieldname } = fieldObj;
                if (fieldhtmltype == 6 && value == "附件上传目录未设置!") {
                    value = "";
                }
                if (issystem == 4) {
                    let tableObj = JSONStr.extendDatas.exmainTable[tableid] || {};
                    tableObj[key] = value;
                    JSONStr.extendDatas.exmainTable[tableid] = tableObj;
                } else {
                    let tableObj = JSONStr.mainTable[tablename] || {};
                    tableObj[fieldname] = value;
                    JSONStr.mainTable[tablename] = tableObj;
                }
            }
        }
        let returnDate = { ...this.feaParam, ...result, data, JSONStr };
        return returnDate
    }


    changeField = (param) => {
        this.form.updateFields(param);
    }
}